﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Magellan.ProjectWizards.Manager.Helpers
{
    public static class VisualStudioHelper
    {
        public static bool EnsureVisualStudioIsNotRunning()
        {
            while (IsVSRunning())
            {
                var result = MessageBox.Show("Visual Studio still appears to be running. Please close all devenv.exe processes and click OK.", "Magellan Setup", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (result == DialogResult.Cancel)
                {
                    return false;
                }
            }
            return true;
        }

        public static string GetCommon7Path()
        {
            var commonTools = Environment.GetEnvironmentVariable("VS90COMNTOOLS");
            var common = Path.GetDirectoryName(Path.GetDirectoryName(commonTools));
            return common;
        }

        public static void Setup()
        {
        	var devEnvPath = Path.Combine(GetCommon7Path(), "IDE");
            var process = Process.Start(devEnvPath + "\\devenv.exe", "/setup");
            process.WaitForExit();
        }

        private static bool IsVSRunning()
        {
            return Process.GetProcesses().FirstOrDefault(x => x.ProcessName.Contains("devenv")) != null;
        }
    }
}
